package com.cy.mall.service.shuidai.impl;

import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.comm.utils.JsonObjectMapper;
import com.cy.mall.model.Result;
import com.cy.mall.model.dto.shuidai.EnterEvaluateRequestParamDTO;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.shuidai.ShuiDaiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description 对接第三方：税贷 service
 * @Author tanyilin
 * @Date 2019/3/21 13:57
 */
@Slf4j
@Service
public class ShuiDaiServiceImpl extends BaseService implements ShuiDaiService {

    @Value("${shuidai.evaluate-url}")
    private String evaluateUrl;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Result<Map<String, Object>> postToEvaluate(Map<String, Object> requestParamMap) {

        //使用当前时间戳作为：唯一业务ID
        String timestamp = String.valueOf(System.currentTimeMillis());
        String bizId = timestamp;

        String paramJson = JsonObjectMapper.toJson(requestParamMap);

        log.info("请求第三方[税贷]api 进行 企业评估，业务参数 paramJson = {}", paramJson);

        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("requestJson", paramJson);
        //请求 json 字符串
        String requestJson = JsonObjectMapper.toJson(requestMap);

        log.info("请求第三方[税贷]api 进行 企业评估，请求报文 param = {}", requestJson);
        log.info("请求第三方[税贷]api 进行 企业评估，bizId = {}", bizId);
        log.info("请求第三方[税贷]api 进行 企业评估，evaluateUrl = {}", evaluateUrl);

        //设置 HttpHeaders、HttpEntity
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("timestamp", timestamp);
        headers.add("bizId", bizId);
        headers.add("appId", "mxs");

        HttpEntity<String> httpEntity = new HttpEntity(requestJson, headers);

        //发送post请求
        ResponseEntity<String> responseEntity = restTemplate.exchange(evaluateUrl, HttpMethod.POST, httpEntity, String.class);

        if (!HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            return Result.fail("请求第三方[税贷]api 进行 企业评估 , 请求失败");
        }

        log.info("请求第三方[税贷]api 进行 企业评估，回调报文 = {}", responseEntity.getBody());

        Map<String, Object> body = JsonObjectMapper.read(responseEntity.getBody(), Map.class);
        boolean success = Boolean.parseBoolean(String.valueOf(body.get("success")));
        String code = String.valueOf(body.get("code"));
        if(!success) {
            return Result.fail("请求第三方[税贷]api 进行 企业评估 , 请求失败，message = " + body.get("message"));
        }
        if(!"9000".equals(code)) {
            return Result.fail("请求第三方[税贷]api 进行 企业评估 , 调用失败，code = " + code);
        }
        //取出 data
        Map<String, Object> data = (Map) body.get("data");
        if(CommonUtils.isEmpty(data)) {
            data = new HashMap<>();
        }
        data.put("bizId", bizId);

        return Result.success("企业评估成功", data);
    }
}
